package detector.home;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import detector.audio.MonitorBar;
import detector.audio.ServerComm;
import detector.audio.TrainActivity;
import detector.audio.processing.AudioClip;
import detector.audio.processing.Monitor;
import detector.audio.processing.PeakHandler;
import detector.audio.processing.SoundClass;

/**
 * Activity for picking a color from a list for a soundclass. Returns RESULT_OK
 * on success
 * 
 * @author Kyle
 * 
 */
public class ColorPickerActivity extends Activity {

	private Integer colors[];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.color_picker);

		// Define the colors we want to be in the list
		colors = new Integer[] { Color.RED, Color.BLUE, Color.GREEN,
				Color.MAGENTA, Color.YELLOW };

		// Get the soundclass the caller wants to change the color for
		final SoundClass toChangeColorFor = (SoundClass) getIntent()
				.getSerializableExtra("detector.audio.processing.SoundClass");

		// Set up the behavior to take when a color is clicked
		ListView lstColors = (ListView) findViewById(R.id.lstColors);
		lstColors.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View scView,
					final int position, long id) {
				// Change the SoundClass's stored color
				toChangeColorFor.setColor(colors[position]);
				// Finish the activity, telling it it was successful
				ColorPickerActivity.this.setResult(RESULT_OK);
				ColorPickerActivity.this.finish();
			}
		});

		// Define how the list of colors is populated and how the
		// items are drawn based on the colors array
		// Store the sounds to an array
		lstColors.setAdapter((ListAdapter) new ColorArrayAdapter(
				ColorPickerActivity.this, R.layout.color_list_item, colors));

	}

	// Adapter for turning the array of colors into views that are displayed in
	// our color list
	private class ColorArrayAdapter extends ArrayAdapter<Integer> {

		public ColorArrayAdapter(Context context, int resource,
				Integer[] objects) {
			super(context, resource, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// Set the view's background color to the color in the object array

			// First, get the inflater so we can intantiate a view using our
			// color_list_item layout
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// Now inflate using our defined color_layout
			View rowView = inflater.inflate(R.layout.color_list_item, parent,
					false);
			// The view that we need to change the background color for is the
			// rowView,
			// so just set its background color to whatever is at the
			// corresponding index
			// in the color array
			rowView.setBackgroundColor(colors[position]);

			return rowView;
		}

	}
}
